package Q482

/**
 * 密钥格式化
 * @author 23737
 * @date 2022.5.12
 */
fun main() {

}

class Solution {
    fun licenseKeyFormatting(S: String, K: Int): String {
        if (K <= 0) {
            return ""
        }
        val builder = StringBuilder()
        for (i in S.indices) {
            if (S[i] != '-') {
                builder.append(S[i])
            }
        }
        var i = builder.length - K
        while (i > 0) {
            builder.insert(i, '-')
            i -= K
        }
        return builder.toString().toUpperCase()
    }
}